﻿/// <reference path="../Turbo/turbo.js" />

var $SortDirection = TA.Turbo.Data.SortDirection =
{
	Ascending: 0,
	Descending: 1
};
var $SortingMode = TA.Turbo.Data.SortingMode =
{
	Automatic: 0,
	ServerOnly: 1
};

TA.Turbo.Data._rgxProperty = new RegExp("[^\\.]+");












TA.Turbo.Data._GetSteps = function _GetSteps(property)
{
	var pSteps = TA.Turbo.Data._rgxProperty.Matches(property);
	for (var i = 0; i < pSteps.length; ++i)
	{
		pSteps[i] = pSteps[i][0];
	}
	return pSteps;
}
TA.Turbo.Data.SetValue = function SetValue(object, property, value)
{
	var pSteps = TA.Turbo.Data._GetSteps(property);
	for (var i = 0; i < pSteps.length - 1; ++i)
	{
		if (object[pSteps[i]] == null)
			object[pSteps[i]] = new Object();
		object = object[pSteps[i]];
	}
	object[pSteps[pSteps.length - 1]] = value;
}
TA.Turbo.Data.GetValue = function GetValue(object, property)
{
	var pSteps = TA.Turbo.Data._GetSteps(property);
	for (var i = 0; i < pSteps.length - 1; ++i)
	{
		if (object == null) return null;
		object = object[pSteps[i]];
	}
	if (object == null) return null;
	return object[pSteps[pSteps.length - 1]];
}
TA.Turbo.Data.GetFieldProperty = function GetFieldProperty(elem)
{
	if (elem.getAttribute("datafield") != null)
		return elem.getAttribute("datafield");
	return null;
}


TA.Turbo.Data.Container = function Container()
{
	this._fields = new Array();
}
TA.Turbo.Data.Container.prototype._fields = new Array();

TA.Turbo.Data.Container.prototype.get_Fields = function get_Fields() { return this._fields; }
TA.Turbo.Data.Container.prototype.AddElement = function AddElement(element)
{
	element.setAttribute("IsDataContainer", "true");
	var obj = this;

	element.PerformRecursive(function AddElement_ProcessOneElement(elem)
	{

		if (elem.get_Container == null)
			return;
		if (elem.get_Container() != obj)
			return;

		var ctlObj = elem.get_Object();
		if (ctlObj == null)
			return;

		if (ctlObj.get_Data == null)
			return;

		var data = ctlObj.get_Data();
		if (data.Field == null || data.Field == "")
			return;
		obj._fields.push(elem);

	}, true);

}
TA.Turbo.Data.Container.prototype.Bind = function Bind(object)
{
	for (var iField = 0; iField < this._fields.length; ++iField)
	{
		var field = this._fields[iField],
			data = field.get_Object().get_Data();
		if ((data.Direction & DataDirection.Bind) != DataDirection.Bind)
			continue;

		var v = TA.Turbo.Data.GetValue(object, data.Field);
		if (v != null)
		{
			if (data.Format != null && data.Format != "")
				v = data.Format.Format(v);
			//			else
			//				v = v.toString();
		}
		field.get_Object().set_Value(v);
	}
}
TA.Turbo.Data.Container.prototype.Store = function Store(object)
{
	for (var iField = 0; iField < this._fields.length; ++iField)
	{
		var field = this._fields[iField],
			data = field.get_Object().get_Data();
		if ((data.Direction & DataDirection.Store) != DataDirection.Store)
			continue;

		var v = field.get_Object().get_Value();
		switch (data.StoringType)
		{
			case DataType.String:
				break;
			case DataType.Integer:
				v = parseInt(v);
				break;
			case DataType.Double:
				v = parseFloat(v);
				break;
			case DataType.Currency:
				v = parseFloat(v);
				break;
			case DataType.Date:
				v = new Date(v);
				break;
			case DataType.Object:
				break;
		}

		TA.Turbo.Data.SetValue(object, data.Field, v);
	}
}





